package com.itfreer.power.service.power;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.itfreer.power.entity.useredit.UserSysAuthoEntity;

/**
 * 定义对外的综合服务接口
 */
@Component
@Path("/power/")
public class RestPowerService {

	/**
	 * 用户信息管理接口
	 */
	@Autowired
	private PowerService powerService;

	/**
	 * 密码验证
	 * 
	 * @return
	 */
	@POST
	@Path("/checkPassword/{uid:.+}/{pwd:.+}")
	@Produces(javax.ws.rs.core.MediaType.APPLICATION_JSON)
	public boolean checkPassword(@PathParam("uid") String userid, @PathParam("pwd") String sPwd) {
		if (powerService == null) {
			return false;
		}
		return powerService.checkPassword(userid, sPwd);
	}
	
	/**
	 * 通过帐号注册
	 * @param accounts
	 * @param password
	 * @param rPassword
	 */
	@POST
	@Path("/userRegister/{ac:.+}/{pw:.+}/{rpw:.+}")
	@Produces(javax.ws.rs.core.MediaType.APPLICATION_JSON)
	public boolean userRegister(@PathParam("ac") String accounts, 
			@PathParam("pw") String password, 
			@PathParam("rpw") String rPassword) {
		return powerService.userRegister(accounts, password, rPassword);
	}

	/**
	 * 获取用户可访问的角色ID集
	 * 
	 * @param userid
	 *            唯一值
	 * @return
	 */
	@POST
	@Path("/getUserRoles")
	@Produces(javax.ws.rs.core.MediaType.APPLICATION_JSON)
	public String getUserRoles(@QueryParam("uid") String userid) {
		if (powerService == null) {
			return null;
		}
		List<String> roles = powerService.getUserRoles(userid);
		if (roles == null || roles.size() < 0) {
			return null;
		}
		String roleString = "";
		for (String role : roles) {
			roleString += role + ",";
		}
		roleString = roleString.substring(0, roleString.length() - 1);
		return roleString;
	}

	/**
	 * 获取用户可访问的应用ID集
	 * 
	 * @param userid
	 *            唯一值
	 * @return
	 */
	@POST
	@Path("/getRoleService")
	@Produces(javax.ws.rs.core.MediaType.APPLICATION_JSON)
	public List<String> getRoleService(@QueryParam("roles") String roles) {
		if (powerService == null) {
			return null;
		}
		List<String> roleList = toList(roles);
		if (roleList == null) {
			return null;
		}
		return powerService.getRoleService(roleList);
	}

	private List<String> toList(String roles) {
		if (roles == null || roles.trim().equals("")) {
			return null;
		}
		List<String> roleList = new ArrayList<String>();
		String[] roleArray = roles.split(",");
		for (String role : roleArray) {
			if (role.trim().equals("")) {
				continue;
			}
			roleList.add(role);
		}
		return roleList;
	}

	/**
	 * 添加用户可访问应用
	 * @param userid
	 * @param service
	 */
	@POST
	@Path("/addUserService")
	@Produces(javax.ws.rs.core.MediaType.APPLICATION_JSON)
	public boolean addUserService(@QueryParam("userid") String userid, @QueryParam("service") String service) {
		if (powerService == null) {
			return false;
		}
		return powerService.addUserService(userid, service);
	}
	
	/**
	 * 移除用户可访问应用
	 * @param userid
	 * @param service
	 */
	@POST
	@Path("/removeUserService")
	@Produces(javax.ws.rs.core.MediaType.APPLICATION_JSON)
	public boolean removeUserService(@QueryParam("userid") String userid, @QueryParam("service") String service) {
		if (powerService == null) {
			return false;
		}
		return powerService.removeUserService(userid, service);
	}
	
	/**
	 * 获取用户可访问菜单ID集
	 * 
	 * @param userid
	 *            唯一值
	 * @param serviceId
	 * @return
	 */
	@POST
	@Path("/getRoleMenus")
	@Produces(javax.ws.rs.core.MediaType.APPLICATION_JSON)
	public List<String> getRoleMenus(@QueryParam("roles") String roles, @QueryParam("sid") String serviceId) {
		if (powerService == null) {
			return null;
		}
		List<String> roleList = toList(roles);
		if (roleList == null) {
			return null;
		}
		return powerService.getRoleMenus(roleList, serviceId);
	}

	/**
	 * 获取用户可访问url地址集
	 * 
	 * @param userid
	 *            唯一值
	 * @param serviceId
	 * @return
	 */
	@POST
	@Path("/getRoleUrls")
	@Produces(javax.ws.rs.core.MediaType.APPLICATION_JSON)
	public List<String> getRoleUrls(@QueryParam("roles") String roles, @QueryParam("sid") String serviceId) {
		if (powerService == null) {
			return null;
		}
		List<String> roleList = toList(roles);
		if (roleList == null) {
			return null;
		}
		return powerService.getRoleUrls(roleList, serviceId);
	}

	/**
	 * 获取用户数据权限集
	 * 
	 * @param userid
	 *            唯一值
	 * @param serviceId
	 * @return
	 */
	@POST
	@Path("/getRoleDatas")
	@Produces(javax.ws.rs.core.MediaType.APPLICATION_JSON)
	public Map<String, List<String>> getRoleDatas(@QueryParam("roles") String roles,
			@QueryParam("sid") String serviceId) {
		if (powerService == null) {
			return null;
		}
		List<String> roleList = toList(roles);
		if (roleList == null) {
			return null;
		}
		return powerService.getRoleDatas(roleList, serviceId);
	}

	/**
	 * 获取用户可访问的地图图层集
	 * 
	 * @param userid
	 *            唯一值
	 * @param serviceId
	 * @return
	 */
	@POST
	@Path("/getRoleMapLayers")
	@Produces(javax.ws.rs.core.MediaType.APPLICATION_JSON)
	public Map<String, List<String>> getRoleMapLayers(@QueryParam("roles") String roles,
			@QueryParam("sid") String serviceId) {
		if (powerService == null) {
			return null;
		}
		List<String> roleList = toList(roles);
		if (roleList == null) {
			return null;
		}
		return powerService.getRoleMapLayers(roleList, serviceId);
	}

	/**
	 * 获取用户可访问的地图范围集
	 * 
	 * @param userid
	 *            唯一值
	 * @param serviceId
	 * @return
	 */
	@POST
	@Path("/getRoleMapExtent")
	@Produces(javax.ws.rs.core.MediaType.APPLICATION_JSON)
	public List<String> getRoleMapExtent(@QueryParam("roles") String roles, @QueryParam("sid") String serviceId) {
		if (powerService == null) {
			return null;
		}
		List<String> roleList = toList(roles);
		if (roleList == null) {
			return null;
		}
		return powerService.getRoleMapExtent(roleList, serviceId);
	}

	@POST
	@Path("/getUserSysAuthoEntity")
	@Produces(javax.ws.rs.core.MediaType.APPLICATION_JSON)
	public List<UserSysAuthoEntity> getUserSysAuthoEntity(@QueryParam("sysId") String serviceId){
		if (powerService == null) {
			return null;
		}
		
		List<UserSysAuthoEntity> UserSysAuthoEntity = powerService.getUserSysAuthoEntitys(serviceId);
		
		return UserSysAuthoEntity;
		
	}
	
}